package com.dd.keel.core.common.i18n;

import com.dd.keel.core.common.utils.EtcConfig;

import java.util.Map;
import java.util.HashMap;
import java.util.Collections;

public class I18nMsg {
	
	private static String I18N_PREFIX = "i18n";
	
	private static String locale = null;
	
	private static Map<String,PropertyMap> i18n_file_map = Collections.synchronizedMap(new HashMap<String,PropertyMap>());
	private static Object lockObj = new Object();
	
	public static void setLocale(String s_locale)
	{
		locale = s_locale;
	}
	
	public static String getLocale()
	{
		if(locale==null)
		{
			locale = EtcConfig.getCommonConfigValue("locale");
		}
		if(locale==null)
		{
			locale = "en_US";
		}
		return locale;
	}
	
	public static String getText(String i18n_prefiex,Class contextClass,String file_prefix,String key)
	{
		String file_name = i18n_prefiex + "/" + file_prefix + "_" + getLocale() + ".properties";
		if(!i18n_file_map.containsKey(file_name))
		{
			synchronized(lockObj)
			{
				if(!i18n_file_map.containsKey(file_name))
				{
					PropertyMap propertyMap = new PropertyMap(contextClass);
					propertyMap.init(file_name);
					i18n_file_map.put(file_name, propertyMap);
				}
			}
		}
		return i18n_file_map.get(file_name).getText(key);
	}	
	
	public static String getText(Class contextClass,String file_prefix,String key)
	{
		return getText(I18N_PREFIX,contextClass,file_prefix,key);
	}
	
	
}
